<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;

class jsamMain{

	public $samerror = '';

	# Data for samDB
	public function samdbSettings($option){
		$conf = $this->settings();
		$host = $conf['sqlip'];
		$port = $conf['sqlport'];
		$user = $conf['sqluser'];
		$pass_protected = $conf['password'];
		$pass = $this->keygen($pass_protected);
		$database = $conf['sqldb'];

		if($option == 'host'){
			if($port == '3306' or $port == ''){
				return $host;
			}
			else{
				return $host.':'.$port;
			}
		}
		else if($option == 'user'){
			return $user;
		}
		else if($option == 'pass'){
			return $pass;
		}
		else if($option == 'db'){
			return $database;
		}
		else{
			return false;
		}
	}

	# Get joosam settings
	protected function settings(){
		$db = JFactory::getDBO();
		$query = "SELECT * FROM #__joosam_settings WHERE id = 1";
		$db->setQuery($query);
		$rows = $db->loadAssoc();
		if($rows){
			return $rows;
		}
		else{
			return false;
		}
	}

	# Get joosam textstring (msgcode need to be given)
	protected function txtstrings($code){
		if($code == ''){
			return false;
		}
		else{
			$db = JFactory::getDBO();
			$query = "SELECT * FROM #__joosam_txt WHERE samerror = '".$code."'";
			$db->setQuery($query);
			$rows = $db->loadAssoc();		
			if($rows){
				return $rows;
			}
			else{
				return false;
			}
		}
	}

	# Get joosam request settings
	protected function requestconf(){
		$db = JFactory::getDBO();
		$query = "SELECT * FROM #__joosam_request WHERE id = 1";
		$db->setQuery($query);
		$rows = $db->loadAssoc();
		if($rows){
			return $rows;
		}
		else{
			return false;
		}
	}
	
	# Get current event (if no event default will be returned)
	protected function events(){
		$conf = $this->settings();
		$tzone = $conf['samsrvtzone'];
		date_default_timezone_set($tzone);
		$time = strtotime('now');
		$date = date('Y-m-d H:i:s', $time);
		$curDB = JFactory::getDBO();
		$curQUERY = "SELECT * FROM #__joosam_events WHERE spstart < '".$date."' AND spends > '".$date."' AND isdef = 'false' ";
		$curDB->setQuery($curQUERY);
		$curRows = $curDB->loadAssoc();
		if($curRows){
			return $curRows;
		}
		else{
			$defDB = JFactory::getDBO();
			$defQUERY = "SELECT * FROM #__joosam_events WHERE isdef = 'true' ";
			$defDB->setQuery($defQUERY);
			$defRows = $defDB->loadAssoc();
			if($defRows){
				return $defRows;
			}
			else{
				return false;
			}
		}
	}

	# joosam keygen. the password to encrypt/decrypt must be given
	protected function keygen($pass){
		# Check empty pass
		if($pass == ''){
			return false;
		}
		else{
			# Get joosem keygen engine
			require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_joosam' . DS . 'helpers' . DS . 'jscrypt.php';
			$engine = new JoosamHelperJscrypt;
			
			# front side don't need encrypt. we use decrypt only

			# when mcrypt is installed
			if(function_exists('mcrypt_module_open')){
				$cleanpass = $engine->jsdecrypt($pass);
			}
			# if mcrypt is deinstalled
			else{
				$cleanpass = $engine->jsbaseDecrypt($pass);
			}
			if($cleanpass){
				return $cleanpass;
			}
			else{
				return false;
			}
		}
	}
}